package org.summerframework.component.security.core.util.useragent;

import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * @author 石超
 * @version v1.0.0
 */
public class UserAgentInfo {
    private BaseEntry browser = new BaseEntry();
    private BaseEntry engine = new BaseEntry();
    private BaseEntry os = new BaseEntry();
    private DeviceEntry device = new DeviceEntry();

    public UserAgentInfo() {
    }

    public UserAgentInfo(ScriptObjectMirror scriptObjectMirror) {
        if (scriptObjectMirror != null) {
            ScriptObjectMirror browser = (ScriptObjectMirror) scriptObjectMirror.get("browser");
            ScriptObjectMirror engine = (ScriptObjectMirror) scriptObjectMirror.get("engine");
            ScriptObjectMirror os = (ScriptObjectMirror) scriptObjectMirror.get("os");
            ScriptObjectMirror device = (ScriptObjectMirror) scriptObjectMirror.get("device");

            this.browser.set(browser);
            this.engine.set(engine);
            this.os.set(os);
            this.device.set(device);
        }
    }

    public String getBrowserName() {
        return browser.getName();
    }

    public String getBrowserVersion() {
        return browser.getVersion();
    }

    public String getEngineName() {
        return engine.getName();
    }

    public String getEngineVersion() {
        return engine.getVersion();
    }

    public String getOsName() {
        return os.getName();
    }

    public String getOsVersion() {
        return os.getVersion();
    }

    public String getDeviceType() {
        return device.getType();
    }

    public String getDeviceManufacturer() {
        return device.getManufacturer();
    }

    public String getDeviceModel() {
        return device.getModel();
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    class BaseEntry {
        private String name = "unknown";
        private String version = "unknown";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public void set(ScriptObjectMirror scriptObjectMirror) {
            if (scriptObjectMirror.hasMember("name")) {
                setName((String) scriptObjectMirror.get("name"));
            }
            if (scriptObjectMirror.hasMember("version") && scriptObjectMirror.get("version") != null) {
                setVersion((String) ((ScriptObjectMirror) scriptObjectMirror.get("version")).get("original"));
            }
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this);
        }
    }

    class DeviceEntry {
        private String type = "unknown";
        private String manufacturer = "unknown";
        private String model = "unknown";

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getManufacturer() {
            return manufacturer;
        }

        public void setManufacturer(String manufacturer) {
            this.manufacturer = manufacturer;
        }

        public String getModel() {
            return model;
        }

        public void setModel(String model) {
            this.model = model;
        }

        public void set(ScriptObjectMirror scriptObjectMirror) {
            if (scriptObjectMirror.hasMember("type")) {
                setType((String) scriptObjectMirror.get("type"));
            }

            if (scriptObjectMirror.hasMember("manufacturer")) {
                setManufacturer((String) scriptObjectMirror.get("manufacturer"));
            }

            if (scriptObjectMirror.hasMember("model")) {
                setModel((String) scriptObjectMirror.get("model"));
            }
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this);
        }
    }
}
